﻿using System;
using WikiPlex;
using WikiPlex.Formatting;

namespace WikiMaml.Renderers
{
    /// <summary>
    /// This will render all text formatting options (bold, italics, underline, subscript, superscript and escaped markup)
    /// </summary>
    public class TextFormattingRenderer : IRenderer
    {
        /// <summary>
        /// Gets the id of a renderer.
        /// </summary>
        public string Id
        {
            get { return "TextFormatting"; }
        }

        /// <summary>
        /// Determines if this renderer can expand the given scope name.
        /// </summary>
        /// <param name="scopeName">The scope name to check.</param>
        /// <returns>
        /// A boolean value indicating if the renderer can or cannot expand the macro.
        /// </returns>
        public bool CanExpand(string scopeName)
        {
            return (scopeName == ScopeName.BoldBegin
                    || scopeName == ScopeName.BoldEnd
                    || scopeName == ScopeName.ItalicsBegin
                    || scopeName == ScopeName.ItalicsEnd
                    || scopeName == ScopeName.UnderlineBegin
                    || scopeName == ScopeName.UnderlineEnd
                    || scopeName == ScopeName.Remove
                    || scopeName == ScopeName.SuperscriptBegin
                    || scopeName == ScopeName.SuperscriptEnd
                    || scopeName == ScopeName.SubscriptBegin
                    || scopeName == ScopeName.SubscriptEnd
                    || scopeName == ScopeName.EscapedMarkup);
        }

        /// <summary>
        /// Will expand the input into the appropriate content based on scope.
        /// </summary>
        /// <param name="scopeName">The scope name.</param>
        /// <param name="input">The input to be expanded.</param>
        /// <param name="htmlEncode">Function that will html encode the output.</param>
        /// <param name="attributeEncode">Function that will html attribute encode the output.</param>
        /// <returns>
        /// The expanded content.
        /// </returns>
        public string Expand(string scopeName, string input, Func<string, string> htmlEncode, Func<string, string> attributeEncode)
        {
            switch (scopeName)
            {
                case ScopeName.BoldBegin:
                    return "<legacyBold>";
                case ScopeName.BoldEnd:
                    return "</legacyBold>";
                case ScopeName.ItalicsBegin:
                    return "<legacyItalic>";
                case ScopeName.ItalicsEnd:
                    return "</legacyItalic>";
                case ScopeName.UnderlineBegin:
                    return "<legacyUnderline>";
                case ScopeName.UnderlineEnd:
                    return "</legacyUnderline>";
                case ScopeName.Remove:
                    return string.Empty;
                case ScopeName.SuperscriptBegin:
                    return "<superscript>";
                case ScopeName.SuperscriptEnd:
                    return "</superscript>";
                case ScopeName.SubscriptBegin:
                    return "<subscript>";
                case ScopeName.SubscriptEnd:
                    return "</subscript>";
                case ScopeName.EscapedMarkup:
                    return htmlEncode(input);
                default:
                    return input;
            }
        }
    }
}